package com.exam.commonbiz.viewmodel;

import android.util.Log;

import androidx.lifecycle.ViewModel;

import com.exam.commonbiz.base.BasicApplication;
import com.exam.commonbiz.bean.MessageInfo;
import com.exam.commonbiz.bean.UserInfo;
import com.exam.commonbiz.model.CommonModel;
import com.exam.commonbiz.model.IBindActivateCodeCallBack;
import com.exam.commonbiz.model.IBindChildPhoneCallBack;
import com.exam.commonbiz.model.IHomeMessageCallBack;
import com.exam.commonbiz.model.ILoginCallBack;
import com.exam.commonbiz.model.IMessageDetailCallBack;
import com.exam.commonbiz.model.IRegisterCallBack;
import com.exam.commonbiz.model.IUserInfoCallBack;
import com.exam.commonbiz.net.BaseResponse;
import com.exam.commonbiz.net.OnLoadListener;
import com.exam.commonbiz.util.Constant;
import com.exam.commonbiz.util.ToastUtil;

import java.util.List;

/**
 * @Author yuexingxing
 * @time 2020/6/11
 */
public class CommonViewModel extends ViewModel {

    private String TAG = CommonViewModel.class.getSimpleName();
    private ILoginCallBack mLoginCallBack;
    private IHomeMessageCallBack mIHomeMessageCallBack;
    private IMessageDetailCallBack mIMessageDetailCallBack;
    private IBindActivateCodeCallBack mIBindActivateCodeCallBack;
    private IUserInfoCallBack mIUserInfoCallBack;
    private IRegisterCallBack mIRegisterCallBack;
    private IBindChildPhoneCallBack mIBindChildPhoneCallBack;

    public void setIBindChildPhoneCallBack(IBindChildPhoneCallBack iBindChildPhoneCallBack) {
        mIBindChildPhoneCallBack = iBindChildPhoneCallBack;
    }

    public void setIRegisterCallBack(IRegisterCallBack iRegisterCallBack) {
        mIRegisterCallBack = iRegisterCallBack;
    }

    public void setLoginCallBack(ILoginCallBack iLoginCallBack) {
        mLoginCallBack = iLoginCallBack;
    }

    public void setIHomeMessageCallBack(IHomeMessageCallBack iHomeMessageCallBack) {
        mIHomeMessageCallBack = iHomeMessageCallBack;
    }

    public void setIMessageDetailCallBack(IMessageDetailCallBack iMessageDetailCallBack) {
        mIMessageDetailCallBack = iMessageDetailCallBack;
    }

    public void setIBindActivateCodeCallBack(IBindActivateCodeCallBack iBindActivateCodeCallBack) {
        mIBindActivateCodeCallBack = iBindActivateCodeCallBack;
    }

    public void setIUserInfoCallBack(IUserInfoCallBack iUserInfoCallBack) {
        mIUserInfoCallBack = iUserInfoCallBack;
    }

    public void getUserInfo(String id) {
        CommonModel.getUserInfo(id, new OnLoadListener<UserInfo>() {


            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse<UserInfo> t) {
                if (mIUserInfoCallBack != null) {
                    BasicApplication.saveUserInfo(t.getInfo());
                    mIUserInfoCallBack.returnUserInfo();
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToastCenter(errMsg);
            }
        });
    }

    public void register(UserInfo userInfo) {
        CommonModel.register(userInfo, new OnLoadListener() {

            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse t) {
                if (mIRegisterCallBack != null) {
                    mIRegisterCallBack.onRegisterSuccess();
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToastCenter(errMsg);
            }
        });
    }

    public void login(UserInfo userInfo) {
        CommonModel.login(userInfo, new OnLoadListener<UserInfo>() {

            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse<UserInfo> t) {
                if (mLoginCallBack != null) {
                    BasicApplication.saveUserInfo(t.getInfo());
                    BasicApplication.app.setToken(t.getInfo().token);
                    mLoginCallBack.onLoginSuccess();
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToastCenter(errMsg);
                if (mLoginCallBack != null) {
                    mLoginCallBack.onLoginFailed();
                }
            }
        });
    }

    public void bindChildPhone(UserInfo userInfo) {
        CommonModel.bindChildPhone(userInfo, new OnLoadListener() {

            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse t) {
                if (mIBindChildPhoneCallBack != null) {
                    mIBindChildPhoneCallBack.returnBindChildPhoneSuccess();
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToastCenter(errMsg);
            }
        });
    }

    public void getMessageMenuList(String masterId) {
        CommonModel.getMessageMenuList(masterId, new OnLoadListener<List<MessageInfo>>() {


            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse<List<MessageInfo>> t) {
                if (mIHomeMessageCallBack != null) {
                    mIHomeMessageCallBack.returnMessageMenList(t.getInfo());
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToast(errMsg);
                if (mIHomeMessageCallBack != null) {
                    mIHomeMessageCallBack.returnMessageMenList(null);
                }
            }
        });
    }

    public void getMessageDetailList(String conversationId) {
        CommonModel.getMessageDetailList(conversationId, new OnLoadListener<List<MessageInfo>>() {


            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse<List<MessageInfo>> t) {
                if (mIMessageDetailCallBack != null) {
                    mIMessageDetailCallBack.returnMessageDetailList(t.getInfo());
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToast(errMsg);
                if (mIMessageDetailCallBack != null) {
                    mIMessageDetailCallBack.returnMessageDetailList(null);
                }
            }
        });
    }

    public void bindActivateCode(String id, UserInfo userInfo) {
        CommonModel.bindActivateCode(id, userInfo, new OnLoadListener() {

            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse t) {
                if (mIBindActivateCodeCallBack != null) {
                    mIBindActivateCodeCallBack.returnBindActivateCode();
                }
            }

            @Override
            public void onLoadFailed(String errMsg) {
                ToastUtil.showLongToastCenter(errMsg);
            }
        });
    }

    public void uploadDeviceToken(UserInfo userInfo) {
        CommonModel.uploadDeviceToken(userInfo, new OnLoadListener() {

            @Override
            public void onLoadStart() {

            }

            @Override
            public void onLoadCompleted() {

            }

            @Override
            public void onLoadSucessed(BaseResponse t) {
                Log.d("PushHelper", "deviceToken上传成功");
                Constant.uploadDeviceToken = true;
            }

            @Override
            public void onLoadFailed(String errMsg) {
                Log.d("PushHelper", errMsg);
                Constant.uploadDeviceToken = false;
            }
        });
    }
}
